<?php

namespace App\Admin\Controllers;

use App\Models\PayConfig;

use App\Models\RechargeRecord;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Form as  WidgetsForm;
use App\Models\User;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Table;

class PayConfigController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(PayConfig::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->disableExport();
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(PayConfig::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
    
    /**
     * 充值配置
     * @return \Encore\Admin\Layout\Content
     */
    public function payconfig()
    {
    	$data = \App\Models\PayConfig::find(1);
    	return Admin::content(function (Content $content) use($data){
    		$content->header('充值管理');
    		
//     		$this->showFormParameters($content);
    		
    		$form = new WidgetsForm($data);
    		
    		$states = [
    				'on'  => ['value' => 'enabled', 'text' => '打开', 'color' => 'success'],
    				'off' => ['value' => 'disabled', 'text' => '关闭', 'color' => 'danger'],
    		];
    		
    		
    		$form->method('post');
    		$form->action('/admin/widgets/payconfig');
    		$form->switch('alipay','支付宝')->default('on')->states($states);
    		$form->switch('wechat','微信')->default('on')->states($states);
    		
    		
    		$form->number('day_frequency', '次数限制(每日)')->rules(function ($form) {
    			
    			if ($form->model()->day_frequency < 0) {
    				return '不能小于0';
    			}
    			
    		});
    		$form->currency('day_amount', '金额限制(每日)')->symbol('￥')->default(100)->rules('required|min:1');
    		
    		$content->body(new Box('充值设置', $form));

    		
    	});
    }
    
    /**
     * 保存充值配置
     */
    public function submitPayConfig(Request $request)
    {
    	$parameters = request()->except(['_pjax', '_token']);
    	
    	$result = \App\Models\PayConfig::updateOrCreate(
    			['id'=>1],$parameters
    	);
    	
    	return ;
    }
    
    
    /**
     * 
     * @param unknown $content
     */
    protected function showFormParameters($content)
    {
    	$parameters = request()->except(['_pjax', '_token']);
    	
    	if (!empty($parameters)) {
    		
    		ob_start();
    		
    		dump($parameters);
    		
    		$contents = ob_get_contents();
    		
    		ob_end_clean();
    		
    		$content->row(new Box('Form parameters', $contents));
    	}
    }
    
    
    
    /**
     * 赠送充值
     * @return \Encore\Admin\Layout\Content
     */
    public function payPresent()
    {
    	return Admin::content(function (Content $content) {
    		$content->header('充值管理');
    		
    		$this->showFormParameters($content);
    		
    		$form = new WidgetsForm();
    		
    		$form->method('post');
    		
    		$form->select('id','用户名')->options(function ($id) {
    			$user = User::find($id);
    			
    			if ($user) {
    				return [$user->id => $user->name];
    			}
    		})->ajax('/admin/api/users');
    		
    		$form->number('coin', '赠送金币数量');
    		
    		$form->action(admin_base_path('widgets/paypresent'));
    		
    		$content->body(new Box('赠送充值', $form));
    	});
    }
    
    /**
     * 赠送充值
     * @return \Encore\Admin\Layout\Content
     */
    public function submitPayPresent()
    {
    	$parameters = request()->except(['_pjax', '_token']);

    	$result = \App\Models\User::where('id',$parameters['id'])->increment('coin', $parameters['coin']);

    	if (!$result)
            admin_toastr('赠送失败', 'error');

    	$record = new RechargeRecord();
    	$record->user_id = $parameters['id'];
    	$record->templet_id = 0;
    	$record->status = 'success';
    	$record->type = 'system';
    	$record->pay_way = 'system';
    	$record->pay_amount = 0;
    	$record->pay_sn = '';
    	$record->amount = $parameters['coin'];
    	$record->save();

        admin_toastr('赠送成功', 'success');
    	return ;
    }
    
    /**
     * @param Request $request
     * @return mixed
     */
    public function users(Request $request)
    {
    	$q = $request->get('q');
    	
    	return User::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
    }
}
